package com.liuyoung.clockview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by Administrator on 2017/2/27.
 */

public class WifiView  extends View {

    private Paint mPaint;

    private Bitmap mWifiBitmap0;
    private Bitmap mWifiBitmap1;
    private Bitmap mWifiBitmap2;
    private Bitmap mWifiBitmap3;
    private Bitmap mWifiBitmap4;

    private Bitmap[] mWifiChanging;

    private int mWifiWidth;
    private int mWifiHeight;

    private Context mContext;

    private int mWifiPower = 0;

    private boolean mIsWifiDisabled = false;

    public WifiView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initWifi();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);//抗锯齿
    }

    private void initWifiChange() {
       mWifiChanging = new Bitmap[4];
       mWifiChanging[3] =  mWifiBitmap4;
       mWifiChanging[2] =  mWifiBitmap3;
       mWifiChanging[1] =  mWifiBitmap2;
       mWifiChanging[0] =  mWifiBitmap1;
    }

    private void initWifi() {
        mWifiBitmap0 = BitmapFactory.decodeResource(getResources(), R.drawable.no_wifi);

        mWifiBitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.wifi_signal_1);
        mWifiBitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.wifi_signal_2);
        mWifiBitmap3 = BitmapFactory.decodeResource(getResources(), R.drawable.wifi_signal_3);
        mWifiBitmap4 = BitmapFactory.decodeResource(getResources(), R.drawable.wifi_signal_4);

        measuereBitmap();
    }

    private void measuereBitmap() {
        mWifiWidth = mWifiBitmap1.getWidth();
        mWifiHeight = mWifiBitmap1.getHeight();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(measureWidth(widthMode, width), measureHeight(heightMode, height));

    }

    @Override
    protected void onDraw(Canvas canvas) {
        Log.d("abc","onDraw");
        if (mIsWifiDisabled) {
            canvas.drawBitmap(mWifiChanging[mWifiPower], 0, 0, mPaint);
        } else {
            canvas.drawBitmap(mWifiBitmap0, 0, 0, mPaint);
            canvas.save();
        }
    }

    private int measureWidth(int mode, int width) {
        switch (mode) {
            case MeasureSpec.UNSPECIFIED:
            case MeasureSpec.AT_MOST:
                break;
            case MeasureSpec.EXACTLY:
                mWifiWidth = width;
                mWifiBitmap0   = Bitmap.createScaledBitmap(mWifiBitmap0,mWifiWidth,mWifiHeight,true);
                mWifiBitmap1   = Bitmap.createScaledBitmap(mWifiBitmap1,mWifiWidth,mWifiHeight,true);
                mWifiBitmap2   = Bitmap.createScaledBitmap(mWifiBitmap2,mWifiWidth,mWifiHeight,true);
                mWifiBitmap3   = Bitmap.createScaledBitmap(mWifiBitmap3,mWifiWidth,mWifiHeight,true);
                mWifiBitmap4   = Bitmap.createScaledBitmap(mWifiBitmap4,mWifiWidth,mWifiHeight,true);
                break;
        }
        initWifiChange();
        return mWifiWidth;
    }

    private int measureHeight(int mode, int height) {
        switch (mode) {
            case MeasureSpec.UNSPECIFIED:
            case MeasureSpec.AT_MOST:
                break;
            case MeasureSpec.EXACTLY:
                mWifiHeight = height;
                break;
        }
        return mWifiHeight;
    }

    public void setPower(int power) {
        mWifiPower = power;
        if (mWifiPower < 0) {
            setIsWifiDisabled(false);
        }
        invalidate();
    }

    public void setIsWifiDisabled(boolean isWifiDisabled) {
        if (mIsWifiDisabled != isWifiDisabled){
            mIsWifiDisabled = isWifiDisabled;
            postInvalidate();
        }
    }

}
